package com.woniu40.petai_manager.web;

import com.woniu40.petai_manager.entity.Manager;
import com.woniu40.petai_manager.entity.Permission;
import com.woniu40.petai_manager.entity.Role;
import com.woniu40.petai_manager.result.ResponseResult;
import com.woniu40.petai_manager.service.ManagerService;
import com.woniu40.petai_manager.vo.PermissionVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.*;

@RestController
@RequestMapping("manager")
@CrossOrigin
@Api("管理员登录")
public class LoginController {
    @Autowired
    ManagerService managerService;

    @PostMapping("/login")
    @ResponseBody
    @ApiOperation("传输map使用其中用户名和密码登录")
    @ApiImplicitParam(name="map",value = "HashMap<String, String>useCode&password",dataType = "int",paramType = "query",required = false)
    public ResponseResult<Manager> login(@RequestBody HashMap<String, String> map) {
        String userCode = map.get("userCode");
        String password = map.get("password");
        System.out.println(userCode);
        Manager manager = managerService.findByName(userCode);
        if (manager == null) {
            return new ResponseResult<>(102, "该用户不存在");
        } else {
            if (manager.getLocked() == 1) {
                if (manager.getPassword().equals(password)) {
                    return new ResponseResult<Manager>(200, "登录成功", manager);
                } else {
                    return new ResponseResult<>(101, "密码错误");
                }
            } else {
                return new ResponseResult<>(500, "账号已被锁死");
            }
        }
    }

    @GetMapping("/home")
    @ResponseBody
    @ApiOperation("通过用户名查找返回权限list")
    @ApiImplicitParam(name="userCode",value = "用户名",dataType = "int",paramType = "query",required = false)
    public ResponseResult<List<PermissionVo>> getRoleMenuListWithTree(String userCode) {
        System.out.println(userCode);
        Manager manager = managerService.findByName(userCode);
        if (manager == null) {
            return new ResponseResult<>(102, "该用户不存在");
        } else {

            List<Role> roles = managerService.findByPrimaryKey(manager.getManagerId());

            if (roles == null) {
                return new ResponseResult<>(102, "角色丢失");
            } else {

                Map<Integer,Permission> map = new HashMap<Integer,Permission>();
                for (Role r: roles){
                    List<Permission> permissions = managerService.findByRoleId(r.getRoleId());
                    for (Permission permission : permissions) {
                        map.put(permission.getPermissionId(),permission);
                    }
                }
                List<PermissionVo> list = new ArrayList<>();
                List<Permission> childs = new ArrayList<>();

                for (Permission v : map.values()) {
                    if (v.getParentId() != -1) {
                        childs.add(v);
                    }
                }

                for (Permission v : map.values()) {
                    List<Permission> child = new ArrayList<>();
                    for (Permission c : childs) {
                        if (v.getParentId() == -1 && (c.getParentId() == v.getPermissionId())) {
                            child.add(c);
                        }
                    }
                    if (v.getParentId() == -1){
                        PermissionVo vo = new PermissionVo(v.getPermissionId(), v.getPermissionName(), child);
                        list.add(vo);
                    }
                }
                return new ResponseResult<List<PermissionVo>>(200, "传输成功", list);
            }
        }
    }
}
